﻿namespace Nomo.UnityCoreModule.AI
{
    /// <summary>
    /// 矩形区域漂浮物。
    /// </summary>
    public sealed class RectFloater2D : Floater
    {
        /// <summary>
        /// 矩形区域。
        /// </summary>
        public UnityEngine.Rect rect;

        /// <inheritdoc />
        protected override bool ShouldUpdateTarget => UnityEngine.Vector3.SqrMagnitude(Current - Target) < 1E-9;

        /// <inheritdoc />
        protected override bool StateValid => rect.width >= 0F && rect.height >= 0F && (rect.width > 0F || rect.height > 0F);

        /// <inheritdoc />
        public override void UpdateTarget()
        {
            var target = Target;
            target.x = UnityEngine.Random.Range(rect.xMin, rect.xMax);
            target.y = UnityEngine.Random.Range(rect.yMin, rect.yMax);
            Target   = target;
        }

        /// <inheritdoc />
        protected override void OnFloat()
        {
            Current = UnityEngine.Vector3.MoveTowards(Current, Target, speed * UnityEngine.Time.deltaTime);
        }
    }
}
